package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.LocalUser;
import com.ngc.mall.common.configure.exception.BusinessException;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.domain.AdminRoleDomain;
import com.ngc.mall.domain.UserDomain;
import com.ngc.mall.dto.UserDTO;
import com.ngc.mall.dto.UserFilterDTO;
import com.ngc.mall.dto.UserPasswordDTO;
import com.ngc.mall.mapper.UserMapper;
import com.ngc.mall.service.IDepartmentService;
import com.ngc.mall.service.IRoleService;
import com.ngc.mall.service.IUserService;
import com.ngc.mall.util.EncryptUtil;
import com.ngc.mall.vo.UserVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDomain> implements IUserService {

    @Resource
    private IRoleService iRoleService;

    @Resource
    private IDepartmentService iDepartmentService;

    @Override
    public UserVO getDetailById(Long id){
        UserDomain domain=super.getById(id);
        return null;
    }

    @Override
    public Boolean resetPassword(Long id, String newPassword) {
        return super.update(Wrappers.<UserDomain>lambdaUpdate().set(UserDomain::getPassword,EncryptUtil.enrypt(newPassword)).eq(UserDomain::getId,id));
    }

    @Override
    public Boolean updateById(Long id, UserDomain domain) {
        domain.setId(id);
        return super.updateById(domain);
    }

    @Override
    public Boolean assignRolesById(Long id, List<Long> roleIdList) {
        baseMapper.deleteRoleByUserId(id);
        return baseMapper.insertRoleIdsByUserId(id,roleIdList);
    }

    @Override
    public List<UserDomain> search(UserFilterDTO dto) {
        LambdaQueryWrapper<UserDomain> queryWrapper= Wrappers.<UserDomain>lambdaQuery().orderByDesc(UserDomain::getCreateTime);
        if(StringUtils.isNotBlank(dto.getNickname())){
            queryWrapper.like(UserDomain::getNickname,dto.getNickname());
        }
        if(dto.getOnline()!=null){
            queryWrapper.eq(UserDomain::getOnline,dto.getOnline());
        }
        return super.list(queryWrapper);
    }

    @Override
    public List<Long> getRoleIdListByUid(Long uid) {
        return baseMapper.selectByUserId(uid);
    }

    @Override
    public List<UserDomain> findAll() {
       return super.list(Wrappers.<UserDomain>lambdaQuery().orderByDesc(UserDomain::getCreateTime));
    }

    @Override
    public UserDomain findUserByAccount(String account) {
        return super.getOne(Wrappers.<UserDomain>lambdaQuery().eq(UserDomain::getAccount,account));
    }

    @Override
    public Boolean login(UserDTO dto) {
        return null;
    }

    @Override
    public Long register(UserDomain domain) {
        Long id=super.create(domain);
        return id;
    }


    @Override
    public Boolean verifyUsernamePassword(Long id,String account,String password){
        UserDomain domain=super.getOne(Wrappers.<UserDomain>lambdaQuery().eq(UserDomain::getAccount,account));
        return EncryptUtil.verify(domain.getPassword(), password);
    }

    @Override
    public UserVO getSelfInfo() {
        ///个人中心
        UserDomain domain= LocalUser.getLocalUser();
        List<Long> roleIdByUidList=this.getRoleIdListByUid(domain.getId());
        List<String> roleList=iRoleService.list(Wrappers.<AdminRoleDomain>lambdaQuery().in(AdminRoleDomain::getId,roleIdByUidList)).stream().map(AdminRoleDomain::getName).collect(Collectors.toList());
        String department=iDepartmentService.getById(domain.getDepartmentId()).getName();
        UserVO userVO= UserVO.builder().nickname(domain.getNickname()).account(domain.getAccount()).mobilePhone(domain.getMobilePhone()).createTime(domain.getCreateTime()).post(domain.getPost()).roleList(roleList).department(department).build();
        return userVO;
    }

    @Override
    public Boolean updateSelfInfo(UserDTO dto){
        Long uid=LocalUser.getLocalUser().getId();
        return super.update(Wrappers.<UserDomain>lambdaUpdate().eq(UserDomain::getId,uid).set(UserDomain::getMobilePhone,dto.getMobilePhone()).set(UserDomain::getNickname,dto.getNickname()));
    }

    @Override
    public Boolean updateSelfPassword(UserPasswordDTO dto) {
        UserDomain domain=LocalUser.getLocalUser();
        Boolean result=EncryptUtil.verify(domain.getPassword(),dto.getOriginalPassword());
        if(result){
            ///旧密码校验失败
           throw BusinessException.FAILURE;
        }
        String newPassword=EncryptUtil.enrypt(dto.getNewPassword());
        return super.update(Wrappers.<UserDomain>lambdaUpdate().eq(UserDomain::getId,domain.getId()).set(UserDomain::getPassword,newPassword));
    }


}
